// 客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h> 
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char const *argv[])
{
    // 1.创建套接字
    int c_fd = socket(AF_INET, SOCK_STREAM, 0);
    // 2.连接服务器
    // 服务端的地址
    struct sockaddr_in s_addr;
    memset(&s_addr, '\0', sizeof(s_addr));
    s_addr.sin_family = AF_INET;
    s_addr.sin_port = htons(8889);
    inet_pton(AF_INET, "127.0.0.1", &s_addr.sin_addr.s_addr);
    int ret = connect(c_fd, (struct sockaddr *)&s_addr, sizeof(s_addr));
    if (ret == -1) {
        perror("服务器未连接\n");
        return -1;
    }
    // 3.请求.响应
    while (1)
    {
        printf("请输入:\n");
        char msg[20];
        fgets(msg, 20, stdin);
        if (!strcmp(msg,"exit\n"))
            break;

    write(c_fd, msg, sizeof(msg));
    char buf[1024];
    memset(buf, '\0', sizeof(buf));
    read(c_fd, buf, 1024);
    printf("收到:%s\n", buf);
}

    // 4.断开连接
    close(c_fd);
    return 0;
}
